import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../router/routers.dart';

class PageHeaderCom extends StatefulWidget implements PreferredSizeWidget {
  final String title;
  final void Function() goBack;
  final bool definedGoBack;
  bool showBottomBorder = true;
  bool showGoBackBtn = true;
  Color bgColor = Colors.white;
  Color titleColor = Color.fromRGBO(51, 51, 51, 1);
  Widget? backIcon;

  PageHeaderCom({
    super.key,
    required this.title,
    this.definedGoBack = false,
    void Function()? goBack,
    this.showBottomBorder = true,
    this.showGoBackBtn = true,
    this.bgColor = Colors.white,
    this.titleColor = const Color.fromRGBO(51, 51, 51, 1),
    this.backIcon,
  }) : goBack = goBack ?? (() {});

  @override
  State<PageHeaderCom> createState() => _PageHeaderComState();

  @override
  Size get preferredSize => Size(400.w, 44.w); //其实这里的值对最后的高度没有什么影响
}

class _PageHeaderComState extends State<PageHeaderCom> {
  @override
  Widget build(BuildContext context) {
    final statusBarHeight = MediaQuery.of(context).padding.top;
    // TODO: implement build
    return Container(
      width: double.infinity,
      height: statusBarHeight + 44,
      decoration: BoxDecoration(
        color: widget.bgColor,
        border: Border(
          bottom: BorderSide(
            color: widget.showBottomBorder ? Color.fromRGBO(243, 242, 247, 1) : Colors.transparent,
            width: 1.w,
          ),
        ),
      ),
      child: Column(
        children: [
          SizedBox(
            height: statusBarHeight,
          ),
          Expanded(
              flex: 1,
              child: Stack(
                children: [
                  Row(
                    children: [
                      Expanded(
                          flex: 1,
                          child: Container(
                            alignment: Alignment.center,
                            child: Text(
                              widget.title,
                              style: TextStyle(
                                fontSize: 16.sp,
                                fontWeight: FontWeight.w500,
                                color: widget.titleColor,
                              ),
                            ),
                          ))
                    ],
                  ),
                  if (widget.showGoBackBtn)
                    Positioned(
                      top: 0,
                      left: 0,
                      child: InkWell(
                        onTap: () {
                          if (widget.definedGoBack) {
                            widget.goBack();
                            return;
                          }
                          Navigator.pop(context);
                        },
                        child: Container(
                          child: (widget.backIcon == null)
                              ? Image.asset(
                                  'assets/images/dapp/history_goback.png',
                                  width: 20.w,
                                  height: 20.w,
                                )
                              : widget.backIcon,
                          padding: EdgeInsets.only(left: 15.w, right: 15.w, top: 12.w, bottom: 12.w),
                        ),
                      ),
                    ),
                ],
              ))
          // Expanded(child: child),
        ],
      ),
    );
  }
}
